define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'upload'], function ($, undefined, Backend, Table, Form, Upload) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'contract/index' + location.search,
                    add_url: 'contract/add',
                    edit_url: 'contract/edit',
                    del_url: 'contract/del',
                    multi_url: 'contract/multi',
                    import_url: 'contract/import',
                    table: 'contract',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name')},
                        {field: 'img_url', title: __('Img_url'), events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'create_time', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'update_time', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            // 初始化图片上传
            Upload.api.plupload("#plupload-img_url", {
                callback: function (data) {
                    // 上传成功后，启用提交按钮
                    $("form[role=form]").validator('validate');
                }
            });
            // 初始化图片选择器
            $("#fachoose-img_url").on('click', function() {
                Fast.api.open('ajax/upload', '选择图片', {
                    callback: function (data) {
                        $("#c-img_url").val(data.url);
                        $("form[role=form]").validator('validate');
                    }
                });
            });
            Controller.api.bindevent();
        },
        edit: function () {
            // 初始化图片上传
            Upload.api.plupload("#plupload-img_url", {
                callback: function (data) {
                    // 上传成功后，启用提交按钮
                    $("form[role=form]").validator('validate');
                }
            });
            // 初始化图片选择器
            $("#fachoose-img_url").on('click', function() {
                Fast.api.open('ajax/upload', '选择图片', {
                    callback: function (data) {
                        $("#c-img_url").val(data.url);
                        $("form[role=form]").validator('validate');
                    }
                });
            });
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
